home *** CD-ROM | disk | FTP | other *** search
/ World of A1200 / World_Of_A1200.iso / programs / graphics / fractals / mandelbrute 1.2 / src.lha / src / MandelBrute.c
C/C++ Source or Header  |  1993-09-24  |  4KB  |  208 lines

  1. /*
  2. Copyright (C) 1993 George Leonidas Coulouris
  3.  
  4. Program    name    : MandelBrute
  5. Description    : Computes an image of (z**2 + c) in the complex plane
  6. Last modified    : 24 Sep 93
  7.  
  8. To do:
  9.     Fix color scaling bug
  10. */
  11.  
  12. /* - Preprocessor ------------------------------------------------ */
  13.  
  14. /*
  15.     USE881 is only set for Amiga users with SAS/C and MC68881s
  16.     COLSPERCOMP <= 256
  17.     NUMCOLS = COLSPERCOMP ** 3
  18. */
  19.  
  20. #define USE881
  21.  
  22. #define NUMCOLS        16777216    /* total number of colors */
  23. #define COLSPERCOMP    256        /* colors per component */
  24.  
  25. #include <stdio.h>
  26. #include <stdlib.h>
  27.  
  28. #ifdef USE881
  29. #include <m68881.h>
  30. #else
  31. #include <math.h>
  32. #endif
  33.  
  34. /* - Declarations ------------------------------------------------ */
  35.  
  36. unsigned short iterations;    /* maximum iterations */
  37.  
  38. double IterateC(double cx,double cy);
  39.  
  40. /* - Main -------------------------------------------------------- */
  41.  
  42. void main(int argc,char *argv[])
  43. {
  44.  
  45. register unsigned short
  46.     x,                /* pixel index */
  47.     y,                /* raster index */
  48.     width,                /* pixelmap width */
  49.     height;                /* pixelmap height */
  50.  
  51. register unsigned long
  52.     tmpr;                /* temporary radius */
  53.  
  54. unsigned char
  55.     tmp;                /* temporary counter */
  56.  
  57. double
  58.     corners[4],            /* radii of corners */
  59.     maxr,                /* maximum radius */
  60.     scalefactor,            /* color scale factor */
  61.     xmin,                /* real minimum */
  62.     xmax,                /* real maximum */
  63.     xfrac,                /* real increment */
  64.     ymin,                /* imaginary minimum */
  65.     ymax,                /* imaginary maximum */
  66.     yfrac;                /* imaginary increment */
  67.  
  68. FILE *fp;
  69.  
  70.                     /* banner */
  71. printf("MandelBrute 1.2 (24 Sep 93)\n");
  72. printf("Copyright (C) 1993 George Leonidas Coulouris\n\n");
  73.  
  74.                     /* check argument list */
  75. if (argc != 9)
  76.     {
  77.     printf("usage: MandelBrute <file> <xmin> <xmax> <ymin> <ymax> <iterations> <width> <height>\n\n");
  78.     exit(0);
  79.     }
  80.  
  81.                     /* check & open file */
  82. if ((fp = fopen(argv[1],"w+")) == NULL)
  83.     {
  84.     printf("File could not be opened.\n\n");
  85.     exit(1);
  86.     }
  87.  
  88.                     /* parse arguments */
  89. xmin        = atof(argv[2]);
  90. xmax        = atof(argv[3]);
  91. ymin        = atof(argv[4]);
  92. ymax        = atof(argv[5]);
  93. iterations    = atoi(argv[6]);
  94. width        = atoi(argv[7]);
  95. height        = atoi(argv[8]);
  96.  
  97.                     /* check arguments */
  98. if ((xmin >= xmax) || (ymin >= ymax) )
  99.     {
  100.     printf("Bad coordinates.\n\n");
  101.     exit(1);
  102.     }
  103.  
  104. if (iterations <= 0)
  105.     {
  106.     printf("Bad iterations.\n\n");
  107.     exit(1);
  108.     }
  109.  
  110. if ( (width <= 0) || (height <= 0) )
  111.     {
  112.     printf("Bad image dimensions.\n\n");
  113.     exit(1);
  114.     }
  115.  
  116.                     /* compute corner radii */
  117. corners[0] = IterateC(xmin,ymin);
  118. corners[1] = IterateC(xmin,ymax);
  119. corners[2] = IterateC(xmax,ymin);
  120. corners[3] = IterateC(xmax,ymax);
  121.  
  122.                     /* find maximum radius */
  123. for(tmp=0;tmp<3;tmp++)
  124.     maxr = (corners[tmp] > corners[tmp+1]) ? corners[tmp] : corners[tmp+1];
  125.  
  126.                     /* compute increments */
  127. xfrac         = fabs((xmax-xmin)) / width;
  128. yfrac         = fabs((ymax-ymin)) / height;
  129.  
  130.                     /* compute color scale factor */
  131. scalefactor    = (NUMCOLS-1) / maxr;
  132.  
  133. /*
  134.     The following four statements write the PPM header.
  135. */
  136.  
  137. fprintf(fp,"P6\n");
  138. fprintf(fp,"%d\n",width);
  139. fprintf(fp,"%d\n",height);
  140. fprintf(fp,"%d\n",(COLSPERCOMP-1));
  141.  
  142. for(y=0;y<height;y++)        /* number crunching loops */
  143.     {
  144.     printf("Line %5d\r",y);
  145.     for(x=0;x<width;x++)
  146.         {
  147.         tmpr = (long)(scalefactor * IterateC( (xmin + x*xfrac) , (ymin + (height-y)*yfrac) ) );
  148.  
  149. /*
  150.     The following three statements extract the red, green, and blue
  151. components of the pixel, and write them to the ppm file.
  152. */
  153.  
  154.         fputc( (int)((COLSPERCOMP-1) * tmpr / (NUMCOLS-1)) ,fp);
  155.  
  156.         fputc( (int)((tmpr/COLSPERCOMP) % COLSPERCOMP), fp);
  157.  
  158.         fputc( (int)(tmpr % COLSPERCOMP) ,fp);
  159.         }
  160.     }
  161.  
  162.                     /* Close up and exit */
  163. if (fclose(fp) == EOF)
  164.     {
  165.     printf("File could not be closed.\n");
  166.     exit(1);
  167.     }
  168. else
  169.     {
  170.     printf("Operation complete.\n");
  171.     exit(0);
  172.     }
  173.  
  174. }
  175.  
  176. /* - Functions --------------------------------------------------- */
  177.  
  178. double IterateC(double cx,double cy)
  179. /*
  180. Arguments    : cx, cy
  181. Value        : 0 if c in Mandelbrot set, else escape radius
  182. */
  183. {
  184. unsigned short i;            /* Iteration index */
  185.  
  186. double
  187.     oldzx = 0,            /* Initial z seeds */
  188.     oldzy = 0,
  189.     zx,
  190.     zy,
  191.     r;
  192.  
  193. for(i=0;i<iterations;i++)
  194.     {
  195.     zx = oldzx * oldzx - oldzy * oldzy + cx;
  196.     zy = 2.0 * oldzx * oldzy + cy;
  197.     r = zx * zx + zy * zy;
  198.  
  199.     if (r > 4.0) return sqrt(r);
  200.     else
  201.         {
  202.         oldzx = zx;
  203.         oldzy = zy;
  204.         }
  205.     }
  206. return 0;
  207. }
  208.